package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityReceiptAddressPrice;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoReceiptAddressPrice;
import com.ocom.payconfig.service.ReceiptAddressPriceService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ReceiptAddressPriceServiceImpl extends ServiceImpl<IDaoReceiptAddressPrice, EntityReceiptAddressPrice> implements ReceiptAddressPriceService {

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean setReceiptAddressPrice(Long companyId, EntityReceiptAddressPrice addressPrice) {
        EntityReceiptAddressPrice entityReceiptAddressPrice = this.getOne(Wrappers.lambdaQuery(EntityReceiptAddressPrice.class)
                .eq(EntityReceiptAddressPrice::getComId,addressPrice.getComId())
                .eq(EntityReceiptAddressPrice::getReceiptId,addressPrice.getReceiptId())
                .eq(EntityReceiptAddressPrice::getPointId,addressPrice.getPointId())
                .last("limit 1")
        );
        if(ObjectUtil.isNotEmpty(entityReceiptAddressPrice)){
            entityReceiptAddressPrice.setPrice(addressPrice.getPrice());
            return this.updateById(entityReceiptAddressPrice);
        }
        return this.save(addressPrice);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public boolean removeReceiptAddressPrice(Long companyId, EntityReceiptAddressPrice addressPrice) {
        return this.remove(Wrappers.lambdaQuery(EntityReceiptAddressPrice.class)
                .eq(EntityReceiptAddressPrice::getComId,addressPrice.getComId())
                .eq(EntityReceiptAddressPrice::getReceiptId,addressPrice.getReceiptId())
                .eq(EntityReceiptAddressPrice::getPointId,addressPrice.getPointId())
        );
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityReceiptAddressPrice> getReceiptAddressPriceList(Long companyId, EntityReceiptAddressPrice addressPrice) {
        return this.list(Wrappers.lambdaQuery(EntityReceiptAddressPrice.class)
                        .select(EntityReceiptAddressPrice::getPointId,EntityReceiptAddressPrice::getPrice,EntityReceiptAddressPrice::getReceiptId)
                .eq(EntityReceiptAddressPrice::getComId,addressPrice.getComId())
                .eq(ObjectUtil.isNotEmpty(addressPrice.getReceiptId()) && addressPrice.getReceiptId() > 0,EntityReceiptAddressPrice::getReceiptId,addressPrice.getReceiptId())
                .eq(ObjectUtil.isNotEmpty(addressPrice.getMerchId()) && addressPrice.getMerchId() > 0,EntityReceiptAddressPrice::getMerchId,addressPrice.getMerchId())
                .eq(ObjectUtil.isNotEmpty(addressPrice.getPointId()) && addressPrice.getPointId() > 0,EntityReceiptAddressPrice::getPointId,addressPrice.getPointId())
        );
    }
}
